﻿using Homework8;
using Homework8.Models;
using Homework8.Services;
using System;

namespace Homework8
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("学生管理系统初始化中...");
            
            try
            {
                // 初始化数据库
                DBHelper.InitializeDatabase();
                Console.WriteLine("数据库初始化成功！");
                
                // 初始化服务
                var schoolService = new SchoolService();
                var classService = new ClassService();
                var studentService = new StudentService();
                var logService = new LogService();
                
                bool exit = false;
                while (!exit)
                {
                    ShowMainMenu();
                    string choice = Console.ReadLine() ?? "";
                    
                    switch (choice)
                    {
                        case "1":
                            ManageSchools(schoolService);
                            break;
                        case "2":
                            ManageClasses(classService);
                            break;
                        case "3":
                            ManageStudents(studentService);
                            break;
                        case "4":
                            ViewLogs(logService);
                            break;
                        case "0":
                            exit = true;
                            break;
                        default:
                            Console.WriteLine("无效选择，请重新输入");
                            break;
                    }
                }
                
                Console.WriteLine("感谢使用学生管理系统，再见！");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"程序错误: {ex.Message}");
            }
        }
        
        static void ShowMainMenu()
        {
            Console.WriteLine("\n=================== 学生管理系统 ===================");
            Console.WriteLine("1. 学校管理");
            Console.WriteLine("2. 班级管理");
            Console.WriteLine("3. 学生管理");
            Console.WriteLine("4. 查看日志");
            Console.WriteLine("0. 退出系统");
            Console.Write("请选择操作: ");
        }
        
        static void ManageSchools(SchoolService schoolService)
        {
            bool back = false;
            while (!back)
            {
                Console.WriteLine("\n=================== 学校管理 ===================");
                Console.WriteLine("1. 查看所有学校");
                Console.WriteLine("2. 查看学校详情");
                Console.WriteLine("3. 添加学校");
                Console.WriteLine("4. 修改学校");
                Console.WriteLine("5. 删除学校");
                Console.WriteLine("0. 返回上级菜单");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine() ?? "";
                
                switch (choice)
                {
                    case "1":
                        var schools = schoolService.GetAllSchools();
                        Console.WriteLine("\n学校列表:");
                        foreach (var school in schools)
                        {
                            Console.WriteLine($"ID: {school.SchoolId}, 名称: {school.Name}, 地址: {school.Address}, 创建年份: {school.FoundingYear}");
                        }
                        break;
                        
                    case "2":
                        Console.Write("请输入学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int schoolId))
                        {
                            var school = schoolService.GetSchoolById(schoolId);
                            if (school != null)
                            {
                                Console.WriteLine($"\n学校详情:");
                                Console.WriteLine($"ID: {school.SchoolId}");
                                Console.WriteLine($"名称: {school.Name}");
                                Console.WriteLine($"地址: {school.Address}");
                                Console.WriteLine($"创建年份: {school.FoundingYear}");
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{schoolId}的学校");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "3":
                        Console.WriteLine("\n添加学校:");
                        Console.Write("请输入学校名称: ");
                        string name = Console.ReadLine() ?? "";
                        Console.Write("请输入学校地址: ");
                        string address = Console.ReadLine() ?? "";
                        Console.Write("请输入创建年份: ");
                        int.TryParse(Console.ReadLine(), out int year);
                        
                        var newSchool = new School { Name = name, Address = address, FoundingYear = year };
                        schoolService.AddSchool(newSchool);
                        Console.WriteLine("学校添加成功！");
                        break;
                        
                    case "4":
                        Console.Write("请输入要修改的学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int updateId))
                        {
                            var existingSchool = schoolService.GetSchoolById(updateId);
                            if (existingSchool != null)
                            {
                                Console.Write($"请输入学校名称 (当前: {existingSchool.Name}): ");
                                string newName = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newName))
                                {
                                    existingSchool.Name = newName;
                                }
                                
                                Console.Write($"请输入学校地址 (当前: {existingSchool.Address}): ");
                                string newAddress = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newAddress))
                                {
                                    existingSchool.Address = newAddress;
                                }
                                
                                Console.Write($"请输入创建年份 (当前: {existingSchool.FoundingYear}): ");
                                string yearStr = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(yearStr) && int.TryParse(yearStr, out int newYear))
                                {
                                    existingSchool.FoundingYear = newYear;
                                }
                                
                                schoolService.UpdateSchool(existingSchool);
                                Console.WriteLine("学校信息已更新！");
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{updateId}的学校");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "5":
                        Console.Write("请输入要删除的学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int deleteId))
                        {
                            var existingSchool = schoolService.GetSchoolById(deleteId);
                            if (existingSchool != null)
                            {
                                Console.Write($"确定要删除学校 \"{existingSchool.Name}\" 吗? (y/n): ");
                                string confirm = Console.ReadLine() ?? "";
                                if (confirm.ToLower() == "y")
                                {
                                    bool success = schoolService.DeleteSchool(deleteId);
                                    if (success)
                                    {
                                        Console.WriteLine("学校已删除！");
                                    }
                                    else
                                    {
                                        Console.WriteLine("删除失败，可能存在关联的班级");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{deleteId}的学校");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "0":
                        back = true;
                        break;
                        
                    default:
                        Console.WriteLine("无效选择，请重新输入");
                        break;
                }
            }
        }
        
        static void ManageClasses(ClassService classService)
        {
            bool back = false;
            while (!back)
            {
                Console.WriteLine("\n=================== 班级管理 ===================");
                Console.WriteLine("1. 查看所有班级");
                Console.WriteLine("2. 查看班级详情");
                Console.WriteLine("3. 查看学校的班级");
                Console.WriteLine("4. 添加班级");
                Console.WriteLine("5. 修改班级");
                Console.WriteLine("6. 删除班级");
                Console.WriteLine("0. 返回上级菜单");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine() ?? "";
                
                switch (choice)
                {
                    case "1":
                        var classes = classService.GetAllClasses();
                        Console.WriteLine("\n班级列表:");
                        foreach (var cls in classes)
                        {
                            Console.WriteLine($"ID: {cls.ClassId}, 名称: {cls.Name}, 年级: {cls.Grade}, 学校ID: {cls.SchoolId}, 班主任: {cls.ClassTeacher}");
                        }
                        break;
                        
                    case "2":
                        Console.Write("请输入班级ID: ");
                        if (int.TryParse(Console.ReadLine(), out int classId))
                        {
                            var cls = classService.GetClassById(classId);
                            if (cls != null)
                            {
                                Console.WriteLine($"\n班级详情:");
                                Console.WriteLine($"ID: {cls.ClassId}");
                                Console.WriteLine($"名称: {cls.Name}");
                                Console.WriteLine($"年级: {cls.Grade}");
                                Console.WriteLine($"学校ID: {cls.SchoolId}");
                                Console.WriteLine($"班主任: {cls.ClassTeacher}");
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{classId}的班级");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "3":
                        Console.Write("请输入学校ID: ");
                        if (int.TryParse(Console.ReadLine(), out int schoolId))
                        {
                            var schoolClasses = classService.GetClassesBySchoolId(schoolId);
                            Console.WriteLine($"\n学校ID {schoolId} 的班级列表:");
                            foreach (var cls in schoolClasses)
                            {
                                Console.WriteLine($"ID: {cls.ClassId}, 名称: {cls.Name}, 年级: {cls.Grade}, 班主任: {cls.ClassTeacher}");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的学校ID");
                        }
                        break;
                        
                    case "4":
                        Console.WriteLine("\n添加班级:");
                        Console.Write("请输入班级名称: ");
                        string name = Console.ReadLine() ?? "";
                        Console.Write("请输入年级: ");
                        int.TryParse(Console.ReadLine(), out int grade);
                        Console.Write("请输入学校ID: ");
                        int.TryParse(Console.ReadLine(), out int schId);
                        Console.Write("请输入班主任: ");
                        string teacher = Console.ReadLine() ?? "";
                        
                        var newClass = new Class { Name = name, Grade = grade, SchoolId = schId, ClassTeacher = teacher };
                        classService.AddClass(newClass);
                        Console.WriteLine("班级添加成功！");
                        break;
                        
                    case "5":
                        Console.Write("请输入要修改的班级ID: ");
                        if (int.TryParse(Console.ReadLine(), out int updateId))
                        {
                            var existingClass = classService.GetClassById(updateId);
                            if (existingClass != null)
                            {
                                Console.Write($"请输入班级名称 (当前: {existingClass.Name}): ");
                                string newName = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newName))
                                {
                                    existingClass.Name = newName;
                                }
                                
                                Console.Write($"请输入年级 (当前: {existingClass.Grade}): ");
                                string gradeStr = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(gradeStr) && int.TryParse(gradeStr, out int newGrade))
                                {
                                    existingClass.Grade = newGrade;
                                }
                                
                                Console.Write($"请输入学校ID (当前: {existingClass.SchoolId}): ");
                                string schIdStr = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(schIdStr) && int.TryParse(schIdStr, out int newSchId))
                                {
                                    existingClass.SchoolId = newSchId;
                                }
                                
                                Console.Write($"请输入班主任 (当前: {existingClass.ClassTeacher}): ");
                                string newTeacher = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newTeacher))
                                {
                                    existingClass.ClassTeacher = newTeacher;
                                }
                                
                                classService.UpdateClass(existingClass);
                                Console.WriteLine("班级信息已更新！");
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{updateId}的班级");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "6":
                        Console.Write("请输入要删除的班级ID: ");
                        if (int.TryParse(Console.ReadLine(), out int deleteId))
                        {
                            var existingClass = classService.GetClassById(deleteId);
                            if (existingClass != null)
                            {
                                Console.Write($"确定要删除班级 \"{existingClass.Name}\" 吗? (y/n): ");
                                string confirm = Console.ReadLine() ?? "";
                                if (confirm.ToLower() == "y")
                                {
                                    bool success = classService.DeleteClass(deleteId);
                                    if (success)
                                    {
                                        Console.WriteLine("班级已删除！");
                                    }
                                    else
                                    {
                                        Console.WriteLine("删除失败，可能存在关联的学生");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{deleteId}的班级");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "0":
                        back = true;
                        break;
                        
                    default:
                        Console.WriteLine("无效选择，请重新输入");
                        break;
                }
            }
        }
        
        static void ManageStudents(StudentService studentService)
        {
            bool back = false;
            while (!back)
            {
                Console.WriteLine("\n=================== 学生管理 ===================");
                Console.WriteLine("1. 查看所有学生");
                Console.WriteLine("2. 查看学生详情");
                Console.WriteLine("3. 查看班级的学生");
                Console.WriteLine("4. 添加学生");
                Console.WriteLine("5. 修改学生");
                Console.WriteLine("6. 删除学生");
                Console.WriteLine("0. 返回上级菜单");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine() ?? "";
                
                switch (choice)
                {
                    case "1":
                        var students = studentService.GetAllStudents();
                        Console.WriteLine("\n学生列表:");
                        foreach (var student in students)
                        {
                            Console.WriteLine($"ID: {student.StudentId}, 姓名: {student.Name}, 年龄: {student.Age}, 性别: {student.Gender}, 班级ID: {student.ClassId}");
                        }
                        break;
                        
                    case "2":
                        Console.Write("请输入学生ID: ");
                        if (int.TryParse(Console.ReadLine(), out int studentId))
                        {
                            var student = studentService.GetStudentById(studentId);
                            if (student != null)
                            {
                                Console.WriteLine($"\n学生详情:");
                                Console.WriteLine($"ID: {student.StudentId}");
                                Console.WriteLine($"姓名: {student.Name}");
                                Console.WriteLine($"年龄: {student.Age}");
                                Console.WriteLine($"性别: {student.Gender}");
                                Console.WriteLine($"班级ID: {student.ClassId}");
                                Console.WriteLine($"入学日期: {student.EnrollmentDate}");
                                Console.WriteLine($"地址: {student.Address}");
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{studentId}的学生");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "3":
                        Console.Write("请输入班级ID: ");
                        if (int.TryParse(Console.ReadLine(), out int classId))
                        {
                            var classStudents = studentService.GetStudentsByClassId(classId);
                            Console.WriteLine($"\n班级ID {classId} 的学生列表:");
                            foreach (var student in classStudents)
                            {
                                Console.WriteLine($"ID: {student.StudentId}, 姓名: {student.Name}, 年龄: {student.Age}, 性别: {student.Gender}");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的班级ID");
                        }
                        break;
                        
                    case "4":
                        Console.WriteLine("\n添加学生:");
                        Console.Write("请输入学生姓名: ");
                        string name = Console.ReadLine() ?? "";
                        Console.Write("请输入年龄: ");
                        int.TryParse(Console.ReadLine(), out int age);
                        Console.Write("请输入性别: ");
                        string gender = Console.ReadLine() ?? "";
                        Console.Write("请输入班级ID: ");
                        int.TryParse(Console.ReadLine(), out int clsId);
                        Console.Write("请输入入学日期 (yyyy-MM-dd): ");
                        string date = Console.ReadLine() ?? "";
                        Console.Write("请输入地址: ");
                        string address = Console.ReadLine() ?? "";
                        
                        var newStudent = new Student { 
                            Name = name, 
                            Age = age, 
                            Gender = gender, 
                            ClassId = clsId,
                            EnrollmentDate = date,
                            Address = address
                        };
                        
                        studentService.AddStudent(newStudent);
                        Console.WriteLine("学生添加成功！");
                        break;
                        
                    case "5":
                        Console.Write("请输入要修改的学生ID: ");
                        if (int.TryParse(Console.ReadLine(), out int updateId))
                        {
                            var existingStudent = studentService.GetStudentById(updateId);
                            if (existingStudent != null)
                            {
                                Console.Write($"请输入学生姓名 (当前: {existingStudent.Name}): ");
                                string newName = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newName))
                                {
                                    existingStudent.Name = newName;
                                }
                                
                                Console.Write($"请输入年龄 (当前: {existingStudent.Age}): ");
                                string ageStr = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(ageStr) && int.TryParse(ageStr, out int newAge))
                                {
                                    existingStudent.Age = newAge;
                                }
                                
                                Console.Write($"请输入性别 (当前: {existingStudent.Gender}): ");
                                string newGender = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newGender))
                                {
                                    existingStudent.Gender = newGender;
                                }
                                
                                Console.Write($"请输入班级ID (当前: {existingStudent.ClassId}): ");
                                string clsIdStr = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(clsIdStr) && int.TryParse(clsIdStr, out int newClsId))
                                {
                                    existingStudent.ClassId = newClsId;
                                }
                                
                                Console.Write($"请输入入学日期 (当前: {existingStudent.EnrollmentDate}): ");
                                string newDate = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newDate))
                                {
                                    existingStudent.EnrollmentDate = newDate;
                                }
                                
                                Console.Write($"请输入地址 (当前: {existingStudent.Address}): ");
                                string newAddress = Console.ReadLine() ?? "";
                                if (!string.IsNullOrEmpty(newAddress))
                                {
                                    existingStudent.Address = newAddress;
                                }
                                
                                studentService.UpdateStudent(existingStudent);
                                Console.WriteLine("学生信息已更新！");
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{updateId}的学生");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "6":
                        Console.Write("请输入要删除的学生ID: ");
                        if (int.TryParse(Console.ReadLine(), out int deleteId))
                        {
                            var existingStudent = studentService.GetStudentById(deleteId);
                            if (existingStudent != null)
                            {
                                Console.Write($"确定要删除学生 \"{existingStudent.Name}\" 吗? (y/n): ");
                                string confirm = Console.ReadLine() ?? "";
                                if (confirm.ToLower() == "y")
                                {
                                    bool success = studentService.DeleteStudent(deleteId);
                                    if (success)
                                    {
                                        Console.WriteLine("学生已删除！");
                                    }
                                    else
                                    {
                                        Console.WriteLine("删除失败");
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine($"未找到ID为{deleteId}的学生");
                            }
                        }
                        else
                        {
                            Console.WriteLine("无效的ID");
                        }
                        break;
                        
                    case "0":
                        back = true;
                        break;
                        
                    default:
                        Console.WriteLine("无效选择，请重新输入");
                        break;
                }
            }
        }
        
        static void ViewLogs(LogService logService)
        {
            bool back = false;
            while (!back)
            {
                Console.WriteLine("\n=================== 日志查询 ===================");
                Console.WriteLine("1. 查看所有日志");
                Console.WriteLine("2. 查看特定表的日志");
                Console.WriteLine("0. 返回上级菜单");
                Console.Write("请选择操作: ");
                
                string choice = Console.ReadLine() ?? "";
                
                switch (choice)
                {
                    case "1":
                        var logs = logService.GetAllLogs();
                        Console.WriteLine("\n日志列表:");
                        foreach (var log in logs)
                        {
                            Console.WriteLine($"ID: {log.LogId}, 操作: {log.Operation}, 表: {log.TableName}, 记录ID: {log.RecordId}, 时间: {log.OperationTime}");
                            Console.WriteLine($"描述: {log.Description}");
                            Console.WriteLine(new string('-', 50));
                        }
                        break;
                        
                    case "2":
                        Console.Write("请输入表名 (School/Class/Student): ");
                        string tableName = Console.ReadLine() ?? "";
                        if (!string.IsNullOrEmpty(tableName))
                        {
                            var tableLogs = logService.GetLogsByTableName(tableName);
                            Console.WriteLine($"\n表 {tableName} 的日志列表:");
                            foreach (var log in tableLogs)
                            {
                                Console.WriteLine($"ID: {log.LogId}, 操作: {log.Operation}, 记录ID: {log.RecordId}, 时间: {log.OperationTime}");
                                Console.WriteLine($"描述: {log.Description}");
                                Console.WriteLine(new string('-', 50));
                            }
                        }
                        break;
                        
                    case "0":
                        back = true;
                        break;
                        
                    default:
                        Console.WriteLine("无效选择，请重新输入");
                        break;
                }
            }
        }
    }
}
